<?php
/**
 * A collection of assorted file system tools
 *
 * Thanks to WordPress for some of these
 * @author WordPress
 */
class FileSystemTools {
	/** Should never be called */
	function FileSystemTools() {
		
	}

	/**
	 * Does various checks to check if $dir is writable
	 * @param string $dir
	 */
	public static function directory_is_writable($dir) {
		return (is_dir($dir) && is_writable($dir));
	}

	/**
	 *
	 * @author WordPress
	 */
	public static function make_temporary_file($filename = '', $dir = ''){
		if ( empty($dir) )
			$dir = FileSystemTools::get_temp_dir();
		$filename = basename($filename);
		if ( empty($filename) )
			$filename = time();

		$filename = $dir . FileSystemTools::unique_filename($dir, $filename);
		//touch($filename);
		return $filename;
	}

	/**
	 *
	 * @author WordPress
	 */
	public static function get_temp_dir() {
		if ( defined('MP_TEMP_DIR') )
			return MP_TEMP_DIR;
 	
		$temp = MP_PATH . '/user/tmp/';
		if(FileSystemTools::directory_is_writable($temp))
			return $temp;

		if  ( function_exists('sys_get_temp_dir') ) {
			$temp_dir = str_replace('\\', '/', sys_get_temp_dir());
			return (substr($temp_dir, -1) == '/' ? $temp_dir : $temp_dir . '/');
		}

		return '/tmp/';
	}

	/**
	 *
	 * @author WordPress
	 * @param string $folder Full path to folder
	 */
	public static function list_files( $folder = '', $levels = 100 ) {
		if( empty($folder) )
			return false;

		if( ! $levels )
			return false;

		$files = array();
		if ( $dir = @opendir( $folder ) ) {
			while (($file = readdir( $dir ) ) !== false ) {
				if ( $file == '.' || $file == '..')
					continue;
				$path = $folder . '/' . $file;
				if ( is_dir( $path ) ) {
					$files2 = FileSystemTools::list_files( $folder . '/' . $file, $levels - 1);
					if( $files2 )
						$files = array_merge($files, $files2 );
					else
						$files[] = $folder . '/' . $file . '/';
				} else {
					$files[] = $path;
				}
			}
		}
		@closedir( $dir );
		return $files;
	}

	/**
	 *
	 * @author WordPress
	 */
	function unique_filename( $dir, $filename, $unique_filename_callback = NULL ) {
		$filename = strtolower( $filename );
		// separate the filename into a name and extension
		$info = pathinfo($filename);
		$ext = $info['extension'];
		$name = basename($filename, ".{$ext}");
		
		// edge case: if file is named '.ext', treat as an empty name
		if( $name === ".$ext" )
			$name = '';

		// Increment the file number until we have a unique file to save in $dir. Use $override['unique_filename_callback'] if supplied.
		if ( $unique_filename_callback && function_exists( $unique_filename_callback ) ) {
			$filename = $unique_filename_callback( $dir, $name );
		} else {
			$number = '';

			if ( empty( $ext ) )
				$ext = '';
			else
				$ext = strtolower( ".$ext" );

			$filename = str_replace( $ext, '', $filename );
			// Strip % so the server doesn't try to decode entities.
			$filename = str_replace('%', '', $filename ) . $ext;

			while ( file_exists( $dir . "/$filename" ) ) {
				if ( '' == "$number$ext" )
					$filename = $filename . ++$number . $ext;
				else
					$filename = str_replace( "$number$ext", ++$number . $ext, $filename );
			}
		}

		return $filename;
}
}